package com.zkh.myframe.tag;

import java.io.IOException;
import java.util.Date;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import com.zkh.myutils.utils.DateUtils;

/**
 * 格式化日期标签实现类，jstl标签格式化时间日期时，有时会出现异常，如 12:30:00.0
 * @author zkh
 *
 */
public class FormatDate extends SimpleTagSupport {
	//值
	private Date value;
	//时间戳
	private Long timestamp;
	//格式
	private String pattern;
	
	/**
	 * 核心方法
	 */
	public void doTag() throws JspException, IOException {
		//输出流
		JspWriter out = this.getJspContext().getOut();
		//先检查时间戳
		if(timestamp!=null){
			out.print(DateUtils.getString(timestamp, pattern));
		}else if(value==null){
			out.print(DateUtils.now(pattern));
		}else{
			out.print(DateUtils.getString(value, pattern));
		}
	}

	public Date getValue() {
		return value;
	}
	public void setValue(Date value) {
		this.value = value;
	}
	public long getTimestamp() {
		return timestamp;
	}
	public void setTimestamp(long timestamp) {
		this.timestamp = timestamp;
	}
	public String getPattern() {
		return pattern;
	}
	public void setPattern(String pattern) {
		this.pattern = pattern;
	}
}
